﻿using HrEFCore;
using HrEFCore.Models;
using HrIService;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HrService
{
    public class EMRService : IEMRService
    {
        private readonly MyDbContext myDbContext;
        public EMRService(MyDbContext myDbConext)
        {
            this.myDbContext = myDbConext;
        }
        public async Task<int> AddEMR(EMR eMR)
        {
            //添加EMR数据
            myDbContext.EMR.AddAsync(eMR);
            return await myDbContext.SaveChangesAsync();
        }
        public List<EMR> CallFenyeStoredProcedure(int pageSize, string keyName, string tableName, string where, int currentPage, out int totalRows)
        {
            // 分页查询
            SqlParameter pageSizeParam = new SqlParameter("@pageSize", pageSize);
            SqlParameter keyNameParam = new SqlParameter("@keyName", keyName);
            SqlParameter tableNameParam = new SqlParameter("@tableName", tableName);
            SqlParameter whereParam = new SqlParameter("@where", where);
            SqlParameter currentPageParam = new SqlParameter("@currentPage", currentPage);
            SqlParameter lieParam = new SqlParameter("@lie", "*");
            SqlParameter rowsParam = new SqlParameter("@rows", SqlDbType.Int);
            rowsParam.Direction = ParameterDirection.Output;
            // 创建一个 StringBuilder 用于构建存储过程调用语句
            var sqlBuilder = new StringBuilder();
            sqlBuilder.Append("EXEC [dbo].[Fenye] @pageSize, @keyName, @tableName, @where, @currentPage, @lie, @rows OUTPUT");
            // 使用 ADO.NET 执行存储过程
            var result = myDbContext.EMR.FromSqlRaw(sqlBuilder.ToString(), pageSizeParam, keyNameParam, tableNameParam, whereParam, currentPageParam, lieParam, rowsParam).ToList();
            totalRows = (int)rowsParam.Value;
            return result;
        }
        public async Task<int> DeleteEMR(short MreId)
        {
            //删除EMR的数据
            var zhi = await myDbContext.EMR.FindAsync(MreId);
            if (zhi == null)
            {
                return 0;
            }
            myDbContext.EMR.Remove(zhi);
            return await myDbContext.SaveChangesAsync();
        }
        public async Task<int> ReviseEMR(EMR eMR)
        {
            //修改EMR数据
            myDbContext.EMR.Update(eMR);
            return await myDbContext.SaveChangesAsync();
        }
        public async Task<EMR> EMRAllAsync(short mreId)
        {
            //根据mreId查询
            return await myDbContext.EMR.FindAsync(mreId);
        }
        public async Task<EMRViewXG> EMRViewXGAllAsync(short mreId)
        {
            var emrData = await myDbContext.EMRViewXG.FirstOrDefaultAsync(e => e.MreId == mreId);
            return emrData;
        }
        public async Task<CFTK> cftkById(string id)
        {
            //三级机构下拉列表,根据id查name，三级
            return await myDbContext.CFTK.Where(e => e.ThirdKindId == id).FirstOrDefaultAsync();
        }

        public async Task<CMK> SelectCMKById(string id)
        {
            //职位名称
            return await myDbContext.CMK.FirstOrDefaultAsync(e => e.MajorKindId == id);
        }

        public async Task<CM> SelectCMById(string id)
        {
            //职位分类名称
            return await myDbContext.CM.FirstOrDefaultAsync(e => e.MajorId == id);
        }
        
        public List<EMRView> GetPagedEMRView(int page, int pageSize)
        {
            var sql = "SELECT MreId, MajorName, SecondKindName, HumanAmount, RegistTime, Deadline FROM [dbo].[EMRView]";
            return myDbContext.EMRView.FromSqlRaw(sql).Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }

        public async Task<List<EMR>> EMRAllAsyncSY(short mreId)
        {
            var emrs = await myDbContext.EMR
                .Where(e => e.MreId == mreId)
                .ToListAsync();
            return emrs;
        }
    }
}